//
//  HQualityDetailViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/6/1.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HQualityDetailViewController.h"
#import "HQc_codeModel.h"
#import "HQCSpecificViewController.h"
#import "HQCDepartViewController.h"
#import "HQualityThreeViewController.h"
#import "Model_Hr_Ns_Inspect_New.h"
#import "InitDbTable.h"
#import "HQualitySave.h"
#import <TMCache.h>
#import "HQualityViewController.h"
@interface HQualityDetailViewController ()
<UITableViewDelegate,
UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *codeDetaliArray;
@property (nonatomic, strong) UILabel *peopleLabel;
@property (nonatomic, strong) UIButton *departButton;
@property (nonatomic, strong) NSMutableArray *arrays;
@property (nonatomic, assign) NSInteger indexPath;
@property (nonatomic, assign) NSInteger indexRow;
@property (nonatomic, strong) NSMutableArray *dept_nameArray;
@property (nonatomic, strong) UITableViewCell *cell;
@property (nonatomic, strong) NSString *level_id;
@property (nonatomic, strong) NSDictionary *menuParam;
@property (nonatomic, copy) NSString *dept_id;
@property (nonatomic, copy) NSString *checkDate;
@property (nonatomic ,strong) Model_Hr_Ns_Inspect_New *inspectModel;

@end

@implementation HQualityDetailViewController

- (instancetype)initWithCodeArray:(NSArray *)codeArray
                  codeDetailArray:(NSArray *)codeDetaliArray
                         para:(NSDictionary *)menuDic
{
    if (self) {
        self.dataArray = codeArray;
        self.menuParam = menuDic;
        self.codeDetaliArray = codeDetaliArray;
        self.arrays = [NSMutableArray new];
        self.dept_nameArray = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(didSaveButttonClicked:)];
    
    
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(RefreshNotice:) name:@"shuaxin" object:nil];
    

    self.checkDate = [super getNowDate];

    [[TMCache sharedCache]setObject:self.checkDate forKey:@"checkDate"];
    
    if ([self.codeModel.PROBLEM_COLLECTION_FLAG isEqualToString:@"1"]) {
        
        self.navigationItem.rightBarButtonItem = execbtn;

    }
    [self.view addSubview:self.tableView];
}


- (void)RefreshNotice:(NSNotification *)sender
{
    InitDbTable* ins = [[InitDbTable alloc]init];
    
    [InitDbTable sharedFMDBManeger];
    
    NSArray *array  = [ins getPar_id:self.rusult_id check_flag:1];
    
    
    if (array.count > 0) {
        
        for (int i = 0; i < self.dataArray.count; i++) {
            
            HQc_codeModel *model = self.dataArray[i];
            for (int j = 0; j < array.count; j ++) {
                
                Model_Hr_InspectDetail_New *detailModel = array[j];
                
                if ([model.CODE_ID isEqualToString:detailModel.par_id]) {
                    
                    model.check_flag = 1;
                }
            }
        }
        
    }
    
    [self.tableView reloadData];
    
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = [[UIView alloc]init];
        
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HQc_codeModel *model = self.dataArray[indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (model.check_flag == 1) {
        
        cell.textLabel.textColor = [UIColor redColor];
    }
    cell.textLabel.text = model.CODE_NAME;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = self.codeDetaliArray[indexPath.row];
    HQc_codeModel *model1 = array[0];
    HQc_codeModel *model = self.dataArray[indexPath.row];
    
    if ([model1.END_FLAG isEqualToString:@"0"]) {

        HQualityThreeViewController *threeVC = [[HQualityThreeViewController alloc]initWithDataArray:array];
        if ([model.PROBLEM_COLLECTION_FLAG isEqualToString:@"1"]) {
            
            threeVC.codeModel = model;
            
        }else {
            threeVC.codeModel = self.codeModel;
        }
        
        threeVC.getBlockInspect_dept = ^(NSString *dept_id) {
            
            self.dept_id = dept_id;
        };
        threeVC.menuParam = self.menuParam;
        threeVC.title = model.CODE_NAME;
        threeVC.result_id = self.rusult_id;
        [self.navigationController pushViewController:threeVC animated:YES];
    }else {
        if ([model.PROBLEM_COLLECTION_FLAG isEqualToString:@"1"]) {
            HQCSpecificViewController *HQCSVC = [[HQCSpecificViewController alloc]initWithSpecificMoel:model Section:indexPath.section dataArray:array level_id:[self.menuParam objectForKey:@"LEVEL_ID"]];
            HQCSVC.title = @"请输入具体情况";
            HQCSVC.result_id = self.rusult_id;
            HQCSVC.type = 3;
            HQCSVC.getBlockInspect = ^(NSString *dept_id) {
                
                self.dept_id= dept_id;
            };
            HQCSVC.getBlockcheckDate = ^(NSString *checkDate) {
              
                
            };
            [self.navigationController pushViewController:HQCSVC animated:YES];
 
        }else {
            HQCSpecificViewController *HQCSVC = [[HQCSpecificViewController alloc]initWithSpecificMoel:self.codeModel Section:indexPath.section dataArray:array level_id:[self.menuParam objectForKey:@"LEVEL_ID"]];
            HQCSVC.title = @"请输入具体情况";
            HQCSVC.result_id = self.rusult_id;
            HQCSVC.type = 4;
            HQCSVC.getBlockInspect = ^(NSString *dept_id) {
                
                self.dept_id = dept_id;
            };
            HQCSVC.getBlockcheckDate = ^(NSString *checkDate) {
                
                
            };

            [self.navigationController pushViewController:HQCSVC animated:YES];

        }
    }
}


- (void)saveQC:(UIButton *)sender
{
    NSLog(@"123");
}

- (void)didSaveButttonClicked:(UIButton *)sender
{
    self.inspectModel = [[Model_Hr_Ns_Inspect_New alloc]init];
    self.inspectModel.RESULT_ID = self.rusult_id;
    //以后从menu中获取
    self.inspectModel.LEVEL_ID = [self.menuParam objectForKey:@"LEVEL_ID"];
    self.inspectModel.TYPE_ID = self.codeModel.TYPE_ID;
    self.inspectModel.REMARKS = @"";
    self.inspectModel.CODE_ID = self.codeModel.CODE_ID;
    self.inspectModel.STANDARD_ID = self.codeModel.STANDARD_ID;
    self.inspectModel.CHECKER_ID = [[TMCache sharedCache]objectForKey:@"CHECK_ID"];
    self.inspectModel.CHECKER_NAME =  [[TMCache sharedCache]objectForKey:@"CHECK_NAME"];
    self.inspectModel.INPUT_USER_ID = [UserDefault getUserInfo].USER_ID;
    self.inspectModel.INPUT_USER_NAME = [UserDefault getUserInfo].USER_NAME;
    self.inspectModel.INPUT_DATE = [super getNowDate];
    self.inspectModel.UPLOAD_FLAG = 0;
    self.inspectModel.NURSE_ID = @"";
    self.inspectModel.NURSE_NAME = @"";
    self.inspectModel.ROUND_HOURS = @"";
    
    InitDbTable *init = [[InitDbTable alloc]init];
    
    NSString *string = [init getMainScore:self.rusult_id];
    
    NSString *reason = [init getMainReason:self.rusult_id];
    
    NSString *reasonID = [init getMainReasonID:self.rusult_id];
    
    
    self.inspectModel.RESUL = reason?:@"";
    
    self.inspectModel.RESION_ID = reasonID?:@"";
    
    CGFloat score = [self.score doubleValue] - [string doubleValue];
    
    self.inspectModel.SCORE = [NSString stringWithFormat:@"%.2f",score];
    
    
    self.inspectModel.CHECK_DATE = self.checkDate;
    
    NSDictionary *dic = [[TMCache sharedCache]objectForKey:@"depart"];
    
    NSString *dept_id = [dic objectForKey:@"department_id"];
    
    self.inspectModel.DEPT_ID = dept_id;

    self.inspectModel.CHECK_DATE = [[TMCache sharedCache]objectForKey:@"checkDate"];
    
    [[HQualitySave alloc]initWithFormData:self.inspectModel];
    
    [HCommon popupSuccessMessage:@"保存成功"];
    
    NSArray *temArray = self.navigationController.viewControllers;
    
    for(UIViewController *temVC in temArray)
        
    {
        
        if ([temVC isKindOfClass:[HQualityViewController class]])
            
        {
            
            [self.navigationController popToViewController:temVC animated:YES];
            
        }
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
